<?php

class Admin_AcademicController extends Zend_Controller_Action
{

    protected $_flashMessenger = null;

    public function init()
    {
       		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			if ($auth->getIdentity ()->role != 'administrator') {
				$this->_helper->redirector ( 'index', 'auth', 'default' );
			}
		} else {
			$this->_helper->redirector ( 'index', 'auth', 'default' );
		}
		$this->_helper->layout->setLayout ( 'admin' );
		$this->_flashMessenger = $this->_helper->getHelper ( 'FlashMessenger' );
		$this->initView ();
    }

    public function indexAction()
    {
         $this->view->messages = $this->_flashMessenger->getMessages();
        // action body
    }

    public function saveAction()
    {
        $acedemic_data = $this->getRequest();
       
        if($acedemic_data->isPost())
        {
            $omAcademic = new Application_Model_Academic();
            $acedemic_data_post = $acedemic_data->getPost();
            $acedemic_insert['start_date'] = $acedemic_data_post['a_start'];
            $acedemic_insert['end_date'] = $acedemic_data_post['a_end'];
            $omAcademic->save($acedemic_insert);
            $this->_flashMessenger->addMessage('Record Saved!');
            $_redirect = $this->_redirect('/admin/academic');
        }
        // action body
    }

    public function academiclistAction() {
		$grid = new Smsgrid_Grid ();
		$examp = $_REQUEST ["q"]; //query number
		$page = $_REQUEST ['page']; // get the requested page
		$limit = $_REQUEST ['rows']; // get how many rows we want to have into the grid
		$sidx = $_REQUEST ['sidx']; // get index row - i.e. user click to sort
		$sord = $_REQUEST ['sord']; // get the direction
		$searchOn = $grid->Strip ( $_REQUEST ['_search'] );

		$wh = '';

		if ($searchOn == 'true') {
			$sarr = $grid->Strip ( $_REQUEST ['filters'] );
			$data = json_decode ( $sarr );
			$finaldata = $data->rules;
			foreach ( $finaldata as $k => $v ) {
				switch ($v->field) {
					case 'start_date' :
					case 'end_date' :
					case 'created' :
						$wh .= " AND " . $v->field . " LIKE '" . $v->data . "%'";
						break;
					case 'is_active' :
					case 'id' :
						$wh .= " AND " . $v->field . " = '" . $v->data . "'";
						break;
				}
			}
		}
		$db = Zend_Registry::get ( 'db' );
		$stmt = $db->query ( 'Select count(*) as count from academic where id !="" ' . $wh . '' );
		$result = $stmt->fetch ();
		$count = $result ['count'];
		if ($count > 0) {
			$total_pages = ceil ( $count / $limit );
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit * $page - $limit; // do not put $limit*($page - 1)
		if ($start < 0)
			$start = 0;
		$query = 'Select * from academic where id !="" ' . $wh . ' ORDER BY ' . $sidx . ' ' . $sord . ' LIMIT ' . $start . ' , ' . $limit;
		$stmt = $db->query ( $query );
		$result = $stmt->fetchAll ();

		$class_list = array ();
		foreach ( $result as $class ) {
			$class_list [] = array ('id' => $class ['id'], 'cell' => array ($class ['id'], $class ['start_date'], $class ['end_date'], $class ['created'] ) );
		}

		$total = $total_pages;

		$final = array ('page' => $page, 'total' => $total, 'records' => $count, 'rows' => ( array ) $class_list );

		die (json_encode($final));
	}


}



